
package com.x52im.rbchat.network.http.async;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.eva.android.NewAsyncTask;
import com.eva.android.widget.WidgetUtils;
import com.eva.framework.dto.DataFromServer;
import com.x52im.rainbowchat.http.logic.dto.RosterElementEntity;
import com.x52im.rbchat.MyApplication;
import com.x52im.rbchat.cache.FriendsListProvider;
import com.x52im.rbchat.logic.alarm.AlarmType;
import com.x52im.rbchat.network.http.HttpRestHelper;

import java.text.MessageFormat;
import com.x52im.rbchat.R;

/**
 * 删除好友异步线程实现类.
 *
 * @author JackJiang
 * @since 8.3
 */
public class DeleteFriendAsync extends NewAsyncTask<RosterElementEntity, Integer, DataFromServer> {

	private final static String TAG = DeleteFriendAsync.class.getSimpleName();

    private final Context context;
    private RosterElementEntity selectedFriend = null;

    public DeleteFriendAsync(Context context) {
        this.context = context;
    }

    @Override
    protected DataFromServer doInBackground(RosterElementEntity... parems) {
        if (parems != null && parems.length > 0) {
            selectedFriend = parems[0];
            return deleteSelectedFriend(selectedFriend);
        }
        return null;
    }

    protected void onPostExecute(DataFromServer dfs) {
        try {
            // 服务端已将其从好友记录中删除成功
            if (dfs != null && dfs.isSuccess()) {
                // 尝试从首页“消息”列表中删除与此人的聊天记录以及存储在本地sqlite的聊天消息等
                int alarmIndex = MyApplication.getInstance(context).getIMClientManager().getAlarmsProvider().getAlarmIndex(AlarmType.friendChat, selectedFriend.getUser_uid());
                MyApplication.getInstance(context).getIMClientManager().getAlarmsProvider().removeAlarm(context, alarmIndex, true, true, true);

                // 从数据模型中删去此好友的数据
                FriendsListProvider rp = MyApplication.getInstance(context).getIMClientManager().getFriendsListProvider();
                if (rp != null && rp.remove(selectedFriend.getUser_uid())) {
                    WidgetUtils.showToast(context, MessageFormat.format(context.getString(R.string.roster_list_delete_return_success), selectedFriend.getNickNameWithRemark()), WidgetUtils.ToastType.OK);
                }

                // 删除完成后跳回到主界面，也就是MainActivity（这个intent规则是在AndroidManifast.xml中配置的，要对应好）
                Intent intent = new Intent(context.getPackageName() + ".main");
                context.startActivity(intent);
            } else {
                WidgetUtils.showToast(context, MessageFormat.format(context.getString(R.string.roster_list_delete_return_failure), selectedFriend.getNickNameWithRemark(), dfs.getReturnValue()), WidgetUtils.ToastType.WARN);
            }
        } catch (Exception e) {
            Log.w(TAG, e);
        }
    }

    /**
     * 提交HTTP请求：删除好友列表中当前选中的好友.
     *
     * @return
     */
    private DataFromServer deleteSelectedFriend(RosterElementEntity selectedFriend) {
        if (selectedFriend != null) {
            return HttpRestHelper.submitDeleteFriendToServer(MyApplication.getInstance(context).getIMClientManager().getLocalUserInfo().getUser_uid(), selectedFriend.getUser_uid());
        }

        return null;
    }
}